/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Analysis;

import java.io.IOException;
import java.util.ArrayList;
import Compaers.*;
import Analysis.NameFindFilter;
import java.io.File;
import java.util.regex.Pattern;
import function.*;
/**
 *
 * @author asfeor
 */

interface Analys{
    public int AnalysisInput(String[] args);
    public String AnalysisExp(String exp); 
}
public class Analysis {
ArrayList <String> List = new ArrayList<String>();
   
public void AnalysisInput(String[] args) throws IOException{// проверяет маску ввода на коректность 
boolean Dir=false,                      // и выбирает вариант ввода
        DirPath=false,
        FindMask=false,
        FindMaskPath=false,
        help=false,
        Sort=false,
        SortPath=false,
        TimeSort=false,
        Tree=false,
        TreePath=false,
        CreateFile=false,
        CreateFileName=false,
        CreateFolder=false,
        CreateFolderName=false,
        OnlyName=false;
int  i=0, j=0;
while (i<args.length){
   if (args[i].equals("-D")) {Dir=true; i++; continue;}
   if (args[i].equals("-F")) {FindMask=true; i++; continue;}
   if (args[i].equals("-CF")){CreateFile=true; i++; continue;}
   if (args[i].equals("-CD")){CreateFolder=true; i++; continue;}
   if (args[i].equals("-T")){TimeSort=true; i++; continue;}
   if (args[i].equals("-S")){Tree=true; i++; continue;}
   if (args[i].equals("-O")){Sort=true; i++; continue;}
   if (args[i].equals("-B")){OnlyName=true; i++; continue;}
   if (Dir&&(i==1)) {DirPath=true; i++; continue;}
   if (DirPath&FindMask&&(i==3)){FindMaskPath=true; i++; continue;}
   if (DirPath&&Tree&&(i==3)) {TreePath=true; i++; continue;}
   if (DirPath&&CreateFile&&(i==3)) {CreateFileName=true; i++; continue;}
   if (DirPath&&CreateFolder&&(i==3)) {CreateFolderName=true; i++; continue;}
   if (DirPath&&FindMaskPath&&Sort&&(i==5)) {SortPath=true; i++; continue;}
   if (DirPath&&TreePath&&Sort&&(i==5)) {SortPath=true; i++; continue;}
   if (!DirPath&FindMask&&(i==1)){FindMaskPath=true; i++; continue;}
   if (!DirPath&&Tree&&(i==1)) {TreePath=true; i++; continue;}
   if (!DirPath&&CreateFile&&(i==1)) {CreateFileName=true; i++; continue;}
   if (!DirPath&&CreateFolder&&(i==1)) {CreateFolderName=true; i++; continue;}
   if (!DirPath&&FindMaskPath&&Sort&&(i==3)) {SortPath=true; i++; continue;}
   if (!DirPath&&TreePath&&Sort&&(i==3)) {SortPath=true; i++; continue;}
   

i++;
}


if (DirPath&&TimeSort&&FindMaskPath) Output(new CompaerLast().sort(Find(args[1], args[3])));
if (!Dir&&FindMaskPath&&TimeSort)  Output(new CompaerLast().sort(Find(new File("").getAbsolutePath(),args[1])));
if (DirPath&&FindMaskPath&&SortPath)  {if (args[5].equals("G")) Output(new CompaerFirsDir().sort(Find(args[1], args[3])));
                                       if (args[5].equals("S")) Output(new CompaerLength().sort(Find(args[1],args[3])));
                                       if (args[5].equals("N")) Output(new CompaerName().sort(Find(args[1],args[3])));
                                        }
if (!Dir&&FindMaskPath&&SortPath)   { String f=new File("").getAbsolutePath();
                                     if (args[5].equals("G")) Output(new CompaerFirsDir().sort(Find(f, args[3])));
                                     if (args[5].equals("S")) Output(new CompaerLength().sort(Find(f,args[3])));
                                     if (args[5].equals("N")) Output(new CompaerName().sort(Find(f,args[3])));
                                    }
if (DirPath&&TreePath&&TimeSort)  Output(new CompaerLast().sort(new function.Trees().Tree(args[1])));
if (!Dir&&TreePath&&TimeSort)  Output(new CompaerLast().sort(new function.Trees().Tree(new File("").getAbsolutePath())));
if (DirPath&&TreePath&&SortPath)  {if (args[5].equals("G")) Output(new CompaerFirsDir().sort(new function.Trees().Tree(args[1])));
                                       if (args[5].equals("S")) Output(new CompaerLength().sort(new function.Trees().Tree(args[1])));
                                       if (args[5].equals("N")) Output(new CompaerName().sort(new function.Trees().Tree(args[1])));
                                        }
if (!Dir&&TreePath&&SortPath)   { String f=new File("").getAbsolutePath();
                                     if (args[5].equals("G")) Output(new CompaerFirsDir().sort(new function.Trees().Tree(f)));
                                     if (args[5].equals("S")) Output(new CompaerLength().sort(new function.Trees().Tree(f)));
                                     if (args[5].equals("N")) Output(new CompaerName().sort(new function.Trees().Tree(f)));
                                    }
if (DirPath&&CreateFileName) new function.Creator().Creat(args[1], args[3],CreateFile,CreateFolder);
if (!Dir&&CreateFileName) { String f=new File("").getAbsolutePath();
                            new function.Creator().Creat(f, args[1], CreateFile, CreateFolder);
                            }
if (DirPath&&CreateFolderName) new function.Creator().Creat(args[1], args[3], CreateFile, CreateFolder);
if (!Dir&&CreateFolderName) { String f=new File("").getAbsolutePath();
                            new function.Creator().Creat(f, args[1], CreateFile, CreateFolder);
                            }
}
 
 public String AnalysisExp(String exp){
char[] s= exp.toCharArray();
int i;  
for (i=0; i<s.length; i++){
  if (s[i]=='*') {exp=exp.substring(0, i-1)+"."+exp.substring(i, exp.length()); i++;}
 }

 return exp;
 } 
 
void Output(ArrayList <String> List){
for (String lst: List){
    File f=new File(lst);
    lst=("Absolute path "+f.getAbsolutePath()+"\n Name "+f.getName()+
          "\n Size"+f.length()+"\n Last modification"+
             new java.util.Date(f.lastModified()));
    System.out.println(lst);
}    
}
private ArrayList <String> Find(String dir, String expr) {

File f = new File(dir);
try{
String[] sDirList = f.list();
int i;
for(i = 0; i < sDirList.length; i++){
 File f1 = new File(dir+File.separator+sDirList[i]);
if (acsept(expr, sDirList[i])) List.add(f1.getAbsolutePath());     
if(f1.isDirectory()) { Find(dir+File.separator+sDirList[i],expr);}        
} } catch (NullPointerException e){ List.add(f.getAbsolutePath());}

return List; 
 }
private boolean  acsept(String expr, String name){
  boolean acses=Pattern.compile(new Analysis().AnalysisExp(expr)).matcher(name).matches();    
return acses;        
}
}
